package com.company.finance.ddd.domain.core.voucher.rule;

import lombok.Getter;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author wanglei
 */
@Getter
public class VoucherType {

    private static final Map<String, VoucherType> VOUCHER_TYPE_CACHE = new ConcurrentHashMap<>();

    private final String code;
    private final String name;

    public VoucherType(String code, String name) {
        this.code = code;
        this.name = name;
    }

    public static Builder builder() {
        return new Builder();
    }

    public static class Builder {
        private String code;
        private String name;

        public Builder code(String code) {
            this.code = code;
            return this;
        }

        public Builder name(String name) {
            this.name = name;
            return this;
        }

        public VoucherType build() {
            if (null == VOUCHER_TYPE_CACHE.get(code)) {
                VOUCHER_TYPE_CACHE.put(code, new VoucherType(code, name));
            }
            return VOUCHER_TYPE_CACHE.get(code);
        }
    }

    @Override
    public int hashCode() {
        return Objects.hash(code, name);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        VoucherType that = (VoucherType) o;
        return Objects.equals(code, that.code) && Objects.equals(name, that.name);
    }

    @Override
    public String toString() {
        return "VoucherType{" +
                "code='" + code + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}
